var app = getApp()
Page({
    data:{
        payAmount: 100.0,
        showPayAmount: '100',
        showBalance: '---.-',
        showMessage: '请选择宿舍',
        showDorm: '未选择',
        dormList:''
    },
    onLoad: function(opt){
        console.log(opt)
        var dormData = opt.data
        if(!dormData){
            this.goLogin()
            return
        }
        var dormList = dormData.split("|")
        if(dormList.length > 1){
            this.setData({
                dormList: dormData,
                cookie: app.globalData.ecardCookie
            })
            
        }else{
            this.goLogin()
        }
    },
    onShow: function(){
        
        var selDorm = wx.getStorageSync('selDorm')
        this.setData({
            selDorm: selDorm
        })
        console.log(selDorm)
        if(selDorm){
            this.setData({
                showDorm: selDorm.name
            })
            this.getBalance()
        }
    },
    getBalance: function(){
      var selDorm = this.data.selDorm
      if(selDorm){
        this.setData({
          showMessage: '正在载入...',
          showBalance: '---.-'
        })
        // 获取电费
        var _this = this
        wx.showNavigationBarLoading()
        wx.request({
          url: 'https://hcdstudio.cn/icard/wx/getBalance.php',
          data: "cookie=" + this.data.cookie + "&building=" + selDorm.dorm + "&floor=" + selDorm.floor + "&room=" + selDorm.room,
          method: 'POST',
          header: {
            "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8;"
          },
          success: function (res) {
            // success
            console.log(res.data)
            var resData = res.data.split(",")
            if (resData.length == 2) {
              _this.setData({
                showMessage: "更新时间: " + resData[1],
                showBalance: resData[0]
              })
            } else {
              _this.setData({
                showMessage: "出错啦/(ㄒoㄒ)/~~"
              })
            }
          },
          fail: function (res) {
            _this.setData({
              showMessage: "出错啦/(ㄒoㄒ)/~~"
            })
          },
          complete: function (res) {
            // complete
            wx.hideNavigationBarLoading()
          }
        })
      }
      
    },
    goLogin: function(){
        wx.showModal({
            title: '提示',
            content: '登陆状态不可用，请重新登陆',
            showCancel: false,
            success: function(res){
                if(res.confirm){
                    wx.reLaunch({
                        url: '../index/index',
                        success: function(res){
                        // success
                        },
                        fail: function(res) {
                        // fail
                        },
                        complete: function(res) {
                        // complete
                        }
                    })
                }
            }
        })
    },
    goSelectDorm: function(){
        wx.navigateTo({
          url: '../selectdorm/selectdorm?data='+this.data.dormList,
          success: function(res){
            // success
          },
          fail: function(res) {
            // fail
          },
          complete: function(res) {
            // complete
          }
        })
    },
    payAmoutInput: function(e){
      var str = e.detail.value
      var flt = parseFloat(str)
      if(flt){
        if(flt >= 1 && flt <= 1000){
          this.setData({
            payAmount: flt.toFixed()
          })
        }
      }
    },
    payAmountEnd: function(){
      this.setData({
        showPayAmount: this.data.payAmount
      })
    },
    goPay: function(){
      var _this = this
      wx.showLoading()
      wx.request({
        url: 'https://hcdstudio.cn/icard/wx/userBalance.php',
        data: "cookie=" + this.data.cookie,
        method: "POST",
        header: {
          "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8;"
        },
        success: function (res) {
          console.log(res.data)
          var modalContent = '宿舍: ' + _this.data.selDorm.name + '\n(' + _this.data.selDorm.raw.floor + ', ' + _this.data.selDorm.raw.room + ') | 充值金额: ' + _this.data.payAmount + '元 | 余额: ' + res.data.balance

          var userBalance = parseFloat(res.data.raw)
          if(userBalance){
            if(userBalance >= _this.data.payAmount){
              wx.showModal({
                title: '电费充值',
                content: modalContent,
                showCancel: true,
                confirmText: '确认充值',
                success: function(confirm){
                  if (confirm.confirm){
                    console.log('confirm pay')
                    console.log("cookie=" + _this.data.cookie + "&dormId=" + _this.data.selDorm.room + "&buildName=" + _this.data.selDorm.raw.build + "&floorName=" + _this.data.selDorm.raw.floor + "&roomName=" + _this.data.selDorm.raw.room + "&money=" + _this.data.payAmount)
                    wx.showLoading()
                    wx.request({
                      url: 'https://hcdstudio.cn/icard/wx/ecpay.php',
                      data: "cookie=" + _this.data.cookie + "&dormId=" + _this.data.selDorm.room + "&buildName=" + _this.data.selDorm.raw.build + "&floorName=" + _this.data.selDorm.raw.floor + "&roomName=" + _this.data.selDorm.raw.room +"&money="+_this.data.payAmount,
                      method: 'POST',
                      header: {
                        "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8;"
                      },
                      success: function(res){
                        console.log(res.data)
                        if(res.data.code == 200){
                          wx.showToast({
                            title: '缴费成功',
                          })
                        }else{
                          wx.showModal({
                            title: '消息',
                            content: res.data.msg,
                            showCancel: false
                          })
                        }
                      },
                      fail: function(){
                        wx.showModal({
                          title: '请求出错',
                          content: '',
                          showCancel: false
                        })
                      },
                      complete: function(){
                        wx.hideLoading()
                      }
                    })
                  }
                }
              })
            }else{
              wx.showModal({
                title: '电费充值',
                content: modalContent,
                showCancel: false,
                confirmText: '余额不足'
              })
            }
          }else{
            wx.showModal({
              title: '请求出错',
              showCancel: false
            })
          }
        },
        fail: function(){
          wx.showModal({
            title: '请求出错',
            showCancel: false
          })
        },
        complete:function(){
          wx.hideLoading()
        }
      })
    },
    exitLogin: function(){
      wx.showModal({
        title: '退出登录?',
        content: '',
        success: function(confirm){
          if(confirm.confirm){
            wx.setStorageSync('ecardCookie', '')
            wx.reLaunch({
              url: '../index/index',
            })
          }
        }
      })
      
    }

})